<!DOCTYPE html><html lang="cn" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>Shell脚本 | 碧蓝幻想</title><meta name="author" content="阿尔托莉雅"><meta name="copyright" content="阿尔托莉雅"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="1.Shell的概念： Shell的定义：  Shell （外壳）是一个用C语言编写的程序，它是用户使用Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。 Shell是指一种应用程序，这个应用程序提供了一个界面，用户通过这个界面访问操作系统内核的服务。   什么时脚本：  脚本简单地说就是一条条的文字命令，这些文字命令是可以看到的（如可以用记事本打开查看、编辑）。 常见的脚本">
<meta property="og:type" content="article">
<meta property="og:title" content="Shell脚本">
<meta property="og:url" content="http://example.com/2023/03/13/3.linux/A4.Shell%E8%84%9A%E6%9C%AC/index.html">
<meta property="og:site_name" content="碧蓝幻想">
<meta property="og:description" content="1.Shell的概念： Shell的定义：  Shell （外壳）是一个用C语言编写的程序，它是用户使用Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。 Shell是指一种应用程序，这个应用程序提供了一个界面，用户通过这个界面访问操作系统内核的服务。   什么时脚本：  脚本简单地说就是一条条的文字命令，这些文字命令是可以看到的（如可以用记事本打开查看、编辑）。 常见的脚本">
<meta property="og:locale">
<meta property="og:image" content="http://example.com/images/Otherwallpaper/avatar.png">
<meta property="article:published_time" content="2023-03-13T12:32:23.823Z">
<meta property="article:modified_time" content="2023-03-13T12:47:12.257Z">
<meta property="article:author" content="阿尔托莉雅">
<meta property="article:tag" content="Linux">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/images/Otherwallpaper/avatar.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2023/03/13/3.linux/A4.Shell%E8%84%9A%E6%9C%AC/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Shell脚本',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-03-13 20:47:12'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/images/Otherwallpaper/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">11</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">2</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archive/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/images/"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/video/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/images/PCwallpaper/backgroud2.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="碧蓝幻想"><span class="site-name">碧蓝幻想</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archive/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/images/"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/video/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Shell脚本</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2023-03-13T12:32:23.823Z" title="Created 2023-03-13 20:32:23">2023-03-13</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2023-03-13T12:47:12.257Z" title="Updated 2023-03-13 20:47:12">2023-03-13</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Linux/">Linux</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Shell脚本"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="1-Shell的概念："><a href="#1-Shell的概念：" class="headerlink" title="1.Shell的概念："></a>1.Shell的概念：</h2><ul>
<li><p>Shell的定义：</p>
<ul>
<li>Shell （外壳）是一个用C语言编写的程序，它是用户使用Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。</li>
<li>Shell是指一种<strong>应用程序</strong>，这个应用程序提供了一个界面，用户通过这个界面访问操作系统内核的服务。</li>
</ul>
</li>
<li><p>什么时脚本：</p>
<ul>
<li>脚本简单地说就是一条条的文字命令，这些文字命令是可以看到的（如可以用记事本打开查看、编辑）。</li>
<li>常见的脚本：JavaScript, VBScript，ASP, JSP, PHP, SQL, Perl, Shell, python, Ruby, JavaFX,Lua等。</li>
</ul>
</li>
<li><p>为什么要学生和使用shell：</p>
<ul>
<li>程序开发的效率非常高，依赖于功能强大的命令可以迅速地完成开发任务</li>
</ul>
</li>
<li><p>常见的shell种类：</p>
<ul>
<li><p>在linux中有很多类型的shell,不同的shell具备不同的功能，shell还决定了脚本中函数的语法，Linux 中默认的shell是&#x2F;bin&#x2F;bash,流行的shell有ash、bash、 ksh、csh、 zsh等，不同的shell都有自己的特点以及用途。</p>
<p>①csh：c shell 使用的是“类C”语法，csh是具有C语言风格的一-种shell, 其内部命令有52个，较为庞大。目前使用的并不多，已经被&#x2F;bin&#x2F;tcsh 所取代。</p>
<p>②ksh：Korn shell的语法与Bourne shell 相同，同时具备了C shell的易用特点。许多安装脚本都使用ksh。ksh有42条内部命令，与bash有一定的限制性。</p>
<p>③tcsh：是csh的增强版，与C shell完全兼容。</p>
<p>④sh：是一个快捷方式，已经被&#x2F;bin&#x2F;bash 所取代。</p>
<p>⑤nologin：指用户不能登录</p>
<p>⑥zsh：目前Linux里最庞大的一种shell：zsh。 它有84个内部命令，使用起来也比较复杂。一般情况下，不会使用该shell.</p>
<p>⑦bash：大多数Linux系统默认使用的shell, bash shell是Bourne shell的一个免费版本，它是早的Unix shell, bash还有一个特点，可以通过help命令来查看帮助。包含的功能几乎可以涵盖shell所具有的功能，所以一般的shell脚本都会指定它为执行路径</p>
</li>
</ul>
</li>
</ul>
<h2 id="2-Shell解析器："><a href="#2-Shell解析器：" class="headerlink" title="2.Shell解析器："></a>2.Shell解析器：</h2><table>
<thead>
<tr>
<th>特殊符号</th>
<th>功能</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>;</td>
<td>可使用分号分隔多条命令</td>
<td>在一行上输入和执行多条较短的命令，可使用分号来分隔命令</td>
</tr>
<tr>
<td>*</td>
<td>匹配任意零个或者多个字符</td>
<td>不能用于创建文件</td>
</tr>
<tr>
<td>？</td>
<td>匹配任意单个字符</td>
<td>不能用于创建文件</td>
</tr>
<tr>
<td>[]</td>
<td>匹配方括号中的任意一个字符，表示范围可以用”-”</td>
<td>不能用于创建文件</td>
</tr>
<tr>
<td>[^]或者[!]</td>
<td>匹配方括号中的任意一个字符或数字后进行取反</td>
<td>用于查找和删除文件，不用于创建文件</td>
</tr>
<tr>
<td>{string,string}</td>
<td>匹配括号中的任意一个字符串，表示一个范围时，字符串之间个”..”</td>
<td>可用于创建、查看、删除文件</td>
</tr>
<tr>
<td>\ s</td>
<td>使\后面的特殊字符失s</td>
<td></td>
</tr>
</tbody></table>
<h3 id="1-普通变量的定义与使用："><a href="#1-普通变量的定义与使用：" class="headerlink" title="1.普通变量的定义与使用："></a>1.普通变量的定义与使用：</h3><ul>
<li><p>定义：(只对本次连接有效)</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">class_name=&quot;yunsuanfu&quot;</span><br><span class="line">_name_4=$(cmd)</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">echo $class_name  # 输出class_name变量</span><br></pre></td></tr></table></figure>
</li>
<li><p>变量名的规范：</p>
<ul>
<li><p>注意，变量名后面的<strong>等号左右不能有空格</strong>，这可能和你熟悉的所有编程语言都不一样</p>
</li>
<li><p>变量名的命名须遵循如下规则：</p>
<ul>
<li><p>命名只能使用英文字母，数字和下划线，首个字符不能以数字开头。</p>
</li>
<li><p>中间不能有空格，可以使用下划线“_”。</p>
</li>
<li><p>不能使用标点符号。</p>
</li>
<li><p>不能使用bash里的关键字（可用help命令查看保留关键字）。</p>
</li>
</ul>
</li>
<li><p><strong>双引号能够识别变量</strong>；单引号是不能识别变量，只会原样输出 </p>
</li>
<li><p><strong>双引号能够实现转义</strong>（类似于“*”）；只会原样输出，单引号是不能转义的</p>
</li>
</ul>
</li>
</ul>
<h3 id="2-环境变量的定义与使用："><a href="#2-环境变量的定义与使用：" class="headerlink" title="2.环境变量的定义与使用："></a>2.环境变量的定义与使用：</h3><ul>
<li><p><strong>环境变量</strong>也可称为全局变量，可以在创建它们的Shell 及其派生出来的任意子进程 Shell 中使用，环境变量又可分为<strong>自定义环境变量</strong>和 <strong>bash</strong> <strong>内置的环境变量</strong>。 </p>
<ul>
<li>bash内置的环境变量：用于定义 Shell 的运行环境，保证 Shell 命令的正确执行，Shell 通过环境变量来确定<strong>登录用户名</strong>、<strong>命令路径</strong>、<strong>终端类型</strong>、<strong>登录目录</strong>等 </li>
<li>自定义环境变量：自定义环境变量可以在命令行中设置和创建，但用户退出命令时这些变量值就会丢失。如果希望永久保存环境变量，可在用户家目录下的 .bash_profile 或 .bashrc 文件中，或者全局配置 &#x2F;etc&#x2F;bashrc 或 &#x2F;etc&#x2F;profile 文件中定义，在将环境变量放入上述的文件中后，每次用户登录时这些变量都将被初始化。</li>
</ul>
</li>
<li><p>所有<strong>环境变量的名字均采用大写形式</strong>。</p>
</li>
<li><p>定义：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">export 变量名=value</span><br></pre></td></tr></table></figure>
</li>
<li><p>通过<strong>echo</strong> <strong>或</strong> <strong>printf</strong> <strong>打印变量</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">echo $HOME</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>显示默认的环境变量</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@www ~]$ env </span><br><span class="line">XDG_SESSION_ID=17 </span><br><span class="line">HOSTNAME=www.oliven.com </span><br><span class="line">TERM=xterm</span><br></pre></td></tr></table></figure>
</li>
<li><p>其他：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@www ~]$ set </span><br><span class="line">BASH=/bin/bash</span><br><span class="line">[root@www ~]$ declare | head</span><br><span class="line">BASH=/bin/bash</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>用</strong> <strong>unset</strong> 消除本地变量和环境变量</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">unset USER</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="3-命令别名："><a href="#3-命令别名：" class="headerlink" title="3.命令别名："></a>3.命令别名：</h3><ul>
<li><p>别名是命令的快捷方式。对于需要经常执行，并需要很长时间输入的长命令创建快捷方式很有用</p>
</li>
<li><p>在命令行中使用alias命令设置的别名仅在该次登入有效，如果重新开启一个 Shell，或者重新登录</p>
<p>系统，则这些alias将无法使用。</p>
</li>
<li><p>语法</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">alias 别名=&#x27;原命令&#x27;</span><br><span class="line">例：</span><br><span class="line">alias myip=&quot;ip a | cut -d&#x27; &#x27; -f6 | head -9 | tail -1&quot;</span><br></pre></td></tr></table></figure>
</li>
<li><p>查看设置的别名：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">alias</span><br></pre></td></tr></table></figure>
</li>
<li><p>删除别名：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">unalias 别名</span><br></pre></td></tr></table></figure>
</li>
<li><p>在linux中提供alias永久化的方法：</p>
<ul>
<li>若要让<strong>某个用户</strong>使用该别名，则把别名加在 ~&#x2F;.bash_profile 或 ~&#x2F;.bashrc 中。然后 source ~&#x2F;.bashrc 或者 source ~&#x2F;.bash_profile </li>
<li>若要让<strong>每一位用户</strong>都使用该别名，则把别名加在 &#x2F;etc&#x2F;bashrc 或者 &#x2F;etc&#x2F;profile 中，然后source &#x2F;etc&#x2F;bashrc 或者 source &#x2F;etc&#x2F;profile</li>
</ul>
</li>
</ul>
<h3 id="4-命令历史："><a href="#4-命令历史：" class="headerlink" title="4.命令历史："></a>4.命令历史：</h3><ul>
<li><p>history 命令可以查阅命令历史记录 ，也可在命令行利用向上或向下光标键来进行查询</p>
</li>
<li><p>语法：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">history [选项] [num]</span><br><span class="line">例：</span><br><span class="line"> history 5     # 查询前5次的命令记录</span><br></pre></td></tr></table></figure>
</li>
<li><p>选项：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">number：显示最近number条命令历史 </span><br><span class="line">-c：清空当前历史命令 </span><br><span class="line">-a [file]：后面没跟文件时，默认将缓冲区中历史命令写入~/.bash_history中 </span><br><span class="line">-r [file]：将历史命令文件中的命令读入当前历史命令缓冲区中 </span><br><span class="line">-w：将当前历史命令缓冲区命令写入历史命令文件中;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="5-内部命令："><a href="#5-内部命令：" class="headerlink" title="5.内部命令："></a>5.内部命令：</h3><ul>
<li><p>echo可在屏幕上输出信息</p>
<table>
<thead>
<tr>
<th>echo参数选项</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-n</td>
<td>不换行输出内容</td>
</tr>
<tr>
<td>-e</td>
<td>解析转义字符</td>
</tr>
</tbody></table>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost ~]# echo -n i have a cat</span><br><span class="line">i have a cat[root@localhost ~]#</span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th>转义字符</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>\n</td>
<td>换行</td>
</tr>
<tr>
<td>\r</td>
<td>回车</td>
</tr>
<tr>
<td>\t</td>
<td>制表符</td>
</tr>
<tr>
<td>\b</td>
<td>退格</td>
</tr>
<tr>
<td>\v</td>
<td>纵向制表符</td>
</tr>
</tbody></table>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost ~]# echo -e &quot;i\thave\ta\tcat&quot;</span><br><span class="line">i have a cat</span><br></pre></td></tr></table></figure>
</li>
<li><p>eval：当shell程序执行到eval语句时，shell读入参数args，并将它们组合成一个新的命令，然后执行（以 ; 为分隔符换行输出数据）</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost test]# a=&#x27;shuju;head -1 /etc/passwd&#x27;</span><br><span class="line">[root@localhost test]# echo $a</span><br><span class="line">shuju;head -1 /etc/passwd</span><br><span class="line">[root@localhost test]# eval echo $a</span><br><span class="line">shuju</span><br><span class="line">root:x:0:0:root:/root:/bin/bash</span><br></pre></td></tr></table></figure>
</li>
<li><p>exec：能够在不创建新的子进程的前提下，转去执行指定的命令，当指定的命令执行完毕后，该进程就终止了</p>
</li>
<li><p>export：设置或者显示环境变量</p>
</li>
<li><p>read：可从标准输入读取字符串等信息，传给shell程序内部定义的变量</p>
<table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>p （prompt）</td>
<td>设置提示信息</td>
</tr>
<tr>
<td>-t （timeout）</td>
<td>设置输入等待时间，单位默认为秒</td>
</tr>
</tbody></table>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost test]# read -t 10 -p &quot;please input your name:&quot; name</span><br><span class="line">please input your name:xiaoming</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">读取两个输入（以空格隔开），分别赋值给name1和name2</span></span><br><span class="line">[root@localhost test]# echo -n &quot;please input your name:&quot;;read name1 name2</span><br><span class="line">please input your name:xiaoming xiaohong</span><br><span class="line">[root@localhost test]# echo $name1</span><br><span class="line">xiaoming</span><br><span class="line">[root@localhost test]# echo $name2</span><br><span class="line">xiaohong</span><br></pre></td></tr></table></figure>
</li>
<li><p>shift：在程序中每使用一次shift语句，都会使所有的位置参数依次向左移动一个位置，并使位置参数$#减1，直到减到0为止</p>
</li>
<li><p>exit：退出shell程序。在exit之后可以有选择地指定一个数作为返回状态</p>
</li>
</ul>
<h2 id="3-shell脚本入门："><a href="#3-shell脚本入门：" class="headerlink" title="3.shell脚本入门："></a>3.shell脚本入门：</h2><h3 id="1-编写规则："><a href="#1-编写规则：" class="headerlink" title="1.编写规则："></a>1.编写规则：</h3><ul>
<li><p>代码规范：      作用：指定告知系统当前这个脚本要使用的shell解释器</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">！/bin/bash</span> </span><br><span class="line">.......</span><br></pre></td></tr></table></figure>
</li>
<li><p>文件命名规范：文件名.sh        .sh是linux下bash shell的默认后缀</p>
</li>
<li><p>开头加版本特权等信息</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># Date:创建日期</span><br><span class="line"># Author:作者</span><br><span class="line"># Mail:联系方式</span><br><span class="line"># Function:功能</span><br><span class="line"># Version:版本</span><br></pre></td></tr></table></figure>
</li>
<li><p>脚本中尽量不要用中文注释</p>
</li>
<li><p>多使用内部命令</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">常用的内部命令有：echo、eval、exec、export、read、shift、exit</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="2-配置vim的编辑格式："><a href="#2-配置vim的编辑格式：" class="headerlink" title="2.配置vim的编辑格式："></a>2.配置vim的编辑格式：</h3><ul>
<li><p>针对单个用户：~&#x2F;.vimrc</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">vim ~/.vimrc</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">内容</span></span><br><span class="line">autocmd BufNewFile *.py,*.cc,*.sh,*.java exec &quot;:call SetTitle()&quot;</span><br><span class="line">  </span><br><span class="line">func SetTitle()</span><br><span class="line">  if expand(&quot;%:e&quot;) == &#x27;sh&#x27;</span><br><span class="line">    call setline(1,&quot;#!/bin/bash&quot;)</span><br><span class="line">        call setline(2,&quot;#########################&quot;)</span><br><span class="line">        call setline(3,&quot;#File name:&quot;.expand(&quot;%&quot;))</span><br><span class="line">        call setline(4,&quot;#Version:v1.0&quot;)</span><br><span class="line">        call setline(5,&quot;#Email:1095322098@qq.com&quot;)</span><br><span class="line">        call setline(6,&quot;#Created time:&quot;.strftime(&quot;%F %T&quot;))</span><br><span class="line">        call setline(7,&quot;#Description:&quot;)</span><br><span class="line">        call setline(8,&quot;#########################&quot;)</span><br><span class="line">        call setline(9,&quot;&quot;)</span><br><span class="line">  endif</span><br><span class="line">endfunc</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">控制缩进格式为4个字符</span></span><br><span class="line">autocmd FileType sh setlocal ai ts=4 sw=4 et</span><br></pre></td></tr></table></figure>
</li>
<li><p>针对所有用户：&#x2F;etc&#x2F;.&#x2F;vimrc</p>
</li>
</ul>
<h3 id="2-注释："><a href="#2-注释：" class="headerlink" title="2.注释："></a>2.注释：</h3><ul>
<li><p>单行注释：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">comment1</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">comment2</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>多行注释：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">:&lt;&lt;BLOCK</span><br><span class="line">……注释内容</span><br><span class="line">BLOCK</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="3-使用流程："><a href="#3-使用流程：" class="headerlink" title="3.使用流程："></a>3.使用流程：</h3><ol>
<li><p>创建一个.sh文件</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">touch test.sh</span><br></pre></td></tr></table></figure>
</li>
<li><p>编写shell代码</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim test.sh</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">！/bin/bash</span> </span><br><span class="line">echo &#x27;hello world!&#x27;</span><br></pre></td></tr></table></figure>
</li>
<li><p>执行shell脚本（脚本必须有执行权限）</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./test.sh  # 相对路径执行</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/usr/local/app/test.sh  # 绝对路径执行</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="4-执行脚本的方法："><a href="#4-执行脚本的方法：" class="headerlink" title="4.执行脚本的方法："></a>4.执行脚本的方法：</h3><ul>
<li><p>查看进程：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ps -ef | grep pts/4</span><br></pre></td></tr></table></figure>
</li>
<li><p>产生<strong>子进程</strong>，再运行，使用<strong>当前指定</strong>的bash shell去运行</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">bash ./filename.sh</span><br></pre></td></tr></table></figure>
</li>
<li><p>产生<strong>子进程</strong>，再运行，使用<strong>脚本里面指定</strong>的shell去运行。使用该种方式执行需要权限</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./filename.sh</span><br></pre></td></tr></table></figure>
</li>
<li><p>（source命令是一个shell内部命令，其功能是读取指定的shell程序文件，并且依次执行其中的所有的语句，并没有创建新的子shell进程，所以脚本里面所有创建的变量都会保存到<strong>当前</strong>的shell里面</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">source ./filename.sh</span><br></pre></td></tr></table></figure>
</li>
<li><p>和source一样，也是使用<strong>当前进程执行</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">. filename.sh</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="5-shell脚本的退出状态："><a href="#5-shell脚本的退出状态：" class="headerlink" title="5.shell脚本的退出状态："></a>5.shell脚本的退出状态：</h3><ul>
<li><p>在UNIX或者Linux中，每个命令都会返回一个退出状态码。退出状态码是一个整数，其有效范围为0~255。</p>
</li>
<li><p>通常情况下，<strong>成功的命令返回0</strong>，而不成功的命令返回非0值。非0值通常都被解释成一个错误码</p>
</li>
<li><p>Shell脚本中的函数和脚本本身也会返回退出状态码。</p>
</li>
<li><p>在脚本或者是脚本函数中执行的最后的命令会决定退出状态码。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">./test1.sh</span><br><span class="line">echo $?</span><br></pre></td></tr></table></figure>
</li>
<li><p>另外，用户也可以在脚本中使用exit语句将指定的退出状态码传递给Shell</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">  </span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="4-变量："><a href="#4-变量：" class="headerlink" title="4.变量："></a>4.变量：</h2><h3 id="1-变量名的规范："><a href="#1-变量名的规范：" class="headerlink" title="1.变量名的规范："></a>1.变量名的规范：</h3><ul>
<li><p>注意，变量名后面的<strong>等号左右不能有空格</strong>，这可能和你熟悉的所有编程语言都不一样</p>
</li>
<li><p>变量名的命名须遵循如下规则：</p>
<ul>
<li><p>命名只能使用英文字母，数字和下划线，首个字符不能以数字开头。</p>
</li>
<li><p>中间不能有空格，可以使用下划线“_”。</p>
</li>
<li><p>不能使用标点符号。</p>
</li>
<li><p>不能使用bash里的关键字（可用help命令查看保留关键字）。</p>
</li>
</ul>
</li>
<li><p><strong>双引号能够识别变量；单引号是不能识别变量</strong>，只会原样输出 </p>
</li>
<li><p><strong>双引号能够实现转义</strong>（类似于“*”）；只会原样输出，<strong>单引号是不能转义</strong>的</p>
</li>
</ul>
<h3 id="2-变量的类型："><a href="#2-变量的类型：" class="headerlink" title="2.变量的类型："></a>2.变量的类型：</h3><ul>
<li><p><strong>根据数据类型分类</strong>：</p>
<ul>
<li><p><strong>Shell</strong>是一种<strong>动态类型语言和弱类型语言</strong>，即在Shell中，变量的数据类型毋需显示地声明，变量的数据类型会根据不同的操作有所变化。</p>
</li>
<li><p>准确地讲，Shell中的变量是不分数据类型的，统一地按照字符串存储。</p>
</li>
<li><p>但是根据变量的上下文环境，允许程序执行一些不同的操作，例如字符串的比较和整数的加减等等</p>
</li>
<li><p>什么是弱类型语言、强类型语言：</p>
<ul>
<li>强类型语言，当你定义一个变量是某个类型，如果不经过代码显式转换（强制转化）过，它就永远都是这个类型</li>
<li>弱类型语言，你想把这个变量当做什么类型来用，就当做什么类型来用，例：js中的var和let</li>
</ul>
</li>
<li><p>declare命令还可输出所有的变量、函数、整数和已经导出的变量</p>
<ul>
<li><p>参数：</p>
<table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-p</td>
<td>显示所有变量的值</td>
</tr>
<tr>
<td>-i</td>
<td>将变量定义为整数，在之后就可以直接对表达式求值，结果只能是整数。如果求值失败或者不是整数，就设置为0</td>
</tr>
<tr>
<td>-r</td>
<td>将变量声明为只读变量。只读变量不允许修改，也不允许删除。(也可使用readonly定义只读变量)</td>
</tr>
<tr>
<td>-a</td>
<td>变量声明为数组变量。但这没有必要，所有变量都不必显示定义就可以用作数组。事实上，在某种意义上，似乎所有变量都是数组，而且赋值给没有下标的变量与赋值给下标为0的数组元素相同</td>
</tr>
<tr>
<td>-f</td>
<td>显示所有自定义函数，包括名称和函数体</td>
</tr>
<tr>
<td>-x</td>
<td>将变量设置成环境变量。可使用+x将变量变成非环境变量</td>
</tr>
</tbody></table>
</li>
</ul>
</li>
</ul>
</li>
<li><p><strong>根据作用域分类</strong>：</p>
<ul>
<li><p>环境变量：</p>
<ul>
<li><h5 id="自定义环境变量："><a href="#自定义环境变量：" class="headerlink" title="自定义环境变量："></a>自定义环境变量：</h5><ul>
<li><p>一般是指用export内置命令导出的变量，用于定义shell的运行环境，保证shell命令的正确执行。<strong>环境变量可以在命令行中设置和创建，但用户退出命令行时这些变量值就会丢失</strong>，即该环境变量只在当前shell和子shell中有效。如果希望永久保存环境变量，可以在配置文件中设置</p>
</li>
<li><p>自定义环境变量的配置文件</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">用户的环境变量配置（non-login shell）</span></span><br><span class="line">~/.bash_profile或~/.bashrc</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">全局环境变量的配置（login shell）</span></span><br><span class="line">/etc/bashrc、/etc/profile文件或者/etc/profile.d目录中定义</span><br></pre></td></tr></table></figure>
</li>
<li><p>注意：按照系统规范，所有环境变量的名字均采用<strong>大写形式</strong>。在将环境变量应用于用户进程程序之前，都应该用命令export导出。</p>
</li>
<li><p>有一些环境变量，比如HOME,PATH,SHELL,UID,USER等，在用户登录前就已经被&#x2F;bin&#x2F;login程序设置好了</p>
</li>
<li><p>通常环境变量被定义并<strong>保存在用户家目录下的.bash_profile文件或全局的配置文件&#x2F;etc&#x2F;profile</strong>中</p>
</li>
</ul>
</li>
<li><h5 id="bash内置的环境变量："><a href="#bash内置的环境变量：" class="headerlink" title="bash内置的环境变量："></a>bash内置的环境变量：</h5><ul>
<li><p>shell内置的环境变量是所有的shell程序都可以使用的变量。shell程序在运行时，都会接收一组变量来确定登录用户名、命令路径、终端类型、登录目录等，这组变量就是环境变量。环境变量会影响到所有的脚本的执行结果</p>
</li>
<li><p>内置的环境变量：</p>
<table>
<thead>
<tr>
<th>变量</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>PATH</td>
<td>命令搜索路径，以冒号为分隔符</td>
</tr>
<tr>
<td>HOME</td>
<td>用户主目录的路径名，是cd命令的默认参数</td>
</tr>
<tr>
<td>COLUMNS</td>
<td>定义了命令编辑模式下可使用命令行的长度</td>
</tr>
<tr>
<td>HISTFILE</td>
<td>命令历史文件</td>
</tr>
<tr>
<td>HISTSIZE</td>
<td>命令历史文件中最多可包含的命令条数</td>
</tr>
<tr>
<td>HISTFILESIZE</td>
<td>命令历史文件中包含的最大行数</td>
</tr>
<tr>
<td>IFS</td>
<td>定义shell使用的分隔符</td>
</tr>
<tr>
<td>LOGNAME</td>
<td>当前的登录名</td>
</tr>
<tr>
<td>SHELL</td>
<td>shell的全路径名</td>
</tr>
<tr>
<td>TERM</td>
<td>终端类型</td>
</tr>
<tr>
<td>TMOUT</td>
<td>shell自动退出的时间，单位为秒，若设为0则禁止shell自动退出</td>
</tr>
<tr>
<td>PWD</td>
<td>当前工作目录</td>
</tr>
</tbody></table>
</li>
<li><p>注：可使用env查看环境变量</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p>普通变量：</p>
<ul>
<li>普通变量也可称为局部变量，与全局变量相比，局部变量的使用范围较小，通常仅限于某个程序段访问，例如函数内部。</li>
<li>在Shell语言中，可以在函数内部通过local关键字定义局部变量，另外，函数的参数也是局部变量</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="3-变量的定义与使用："><a href="#3-变量的定义与使用：" class="headerlink" title="3.变量的定义与使用："></a>3.变量的定义与使用：</h3><ul>
<li><p>变量定义示例：变量名&#x3D;变量值</p>
<ul>
<li><p>普通变量的定义：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">定义数值变量</span></span><br><span class="line">a=1</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">定义字符串变量</span></span><br><span class="line">[变量]=&quot;yunsuanfu&quot;</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">定义备份路径</span></span><br><span class="line">bak_dir=/data/backup</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">把一个命令的结果赋值变量</span></span><br><span class="line">变量名=`ls`</span><br><span class="line">变量名=$(ls)</span><br></pre></td></tr></table></figure>
</li>
<li><p>只读变量的定义</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">readonly a=10</span><br></pre></td></tr></table></figure>
</li>
<li><p>接受用户输入：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">read -p [提示信息] [变量名]</span><br><span class="line"># 例：read -p &quot;请输入需要创建的文件路径：&quot; filePath</span><br></pre></td></tr></table></figure>
</li>
<li><p>变量的删除：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">unset [变量]</span><br></pre></td></tr></table></figure>
</li>
<li><p>将变量转换成环境变量</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">export mydata2=mydata2</span><br><span class="line">mydata=mydata;export mydata</span><br><span class="line">declare -x mydata3=mydata3</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>位置参数和预定义变量:</p>
<ul>
<li><p>许多情况下，Shell脚本都需要接收用户的输入，根据用户输入的参数来执行不同的操作</p>
</li>
<li><p>从命令行传递给Shell脚本的参数又称为位置参数，Shell脚本会根据参数的位置使用不同的位置参数变量读取它们的值:</p>
<table>
<thead>
<tr>
<th>变量</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>$#</td>
<td>命令行参数的个数</td>
</tr>
<tr>
<td>$n</td>
<td>表示传递给脚本的第n个参数，例如$1表示第一个参数，$2表示第二个参数，$3表示第三个参数</td>
</tr>
<tr>
<td>$0</td>
<td>当前脚本的名称</td>
</tr>
<tr>
<td>$*</td>
<td>以”参数1 参数2 参数3…”的形式返回所有参数的值</td>
</tr>
<tr>
<td>$@</td>
<td>以“参数1”“参数2”“参数3”…的形式返回所有的值</td>
</tr>
<tr>
<td>$?</td>
<td>前一个命令或者函数的返回状态码</td>
</tr>
<tr>
<td>$$</td>
<td>返回本程序的进程ID（PID），不常用</td>
</tr>
<tr>
<td>$!</td>
<td>获取上一个在后台工作的进程的进程号，不常用</td>
</tr>
<tr>
<td>$_</td>
<td>保存之前执行的命令的最后一个参数，不常用</td>
</tr>
</tbody></table>
</li>
<li><p>示例：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;第1个位置参数是$1&quot;</span><br><span class="line">echo &quot;第2个位置参数是$2&quot;</span><br><span class="line">echo &quot;所有参数是: $*&quot;</span><br><span class="line">echo &quot;所有参数是: $@&quot;</span><br><span class="line">echo &quot;参数的个数是: $#&quot;</span><br><span class="line">echo &quot;当前进程的PID是: $$&quot;</span><br><span class="line">[root@localhost test]# bash param.sh shuju1 shuju2</span><br><span class="line">第1个位置参数是shuju1</span><br><span class="line">第2个位置参数是shuju2</span><br><span class="line">所有参数是: shuju1 shuju2</span><br><span class="line">所有参数是: shuju1 shuju2</span><br><span class="line">参数的个数是: 2</span><br><span class="line">当前进程的PID是: 23278</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">ping -c2 $1 &amp;&gt; /dev/null</span><br><span class="line">if [ $? = 0 ];then           # $?为上一个命令或函数返回的状态码</span><br><span class="line">echo &quot;host $1 is ok&quot;      </span><br><span class="line">else</span><br><span class="line">echo &quot;host $1 is fail&quot;</span><br><span class="line">fi</span><br><span class="line">[root@localhost test]# chmod a+rx ping.sh</span><br><span class="line">[root@localhost test]# ./ping.sh 192.168.168.128</span><br><span class="line">host 192.168.168.128 is ok</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost test]# for i in &quot;$*&quot;;do echo $i;done</span><br><span class="line">i have a cat</span><br><span class="line">[root@localhost test]# for i in &quot;$@&quot;;do echo $i;done</span><br><span class="line">i have</span><br><span class="line">a</span><br><span class="line">cat</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost test]# shift #使用该指令可将位置参数左移一位</span><br><span class="line">[root@localhost test]# echo $1</span><br><span class="line">a</span><br><span class="line">[root@localhost test]# shift</span><br><span class="line">[root@localhost test]# echo $1</span><br><span class="line">cat</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<h3 id="4-shell中变量的引用："><a href="#4-shell中变量的引用：" class="headerlink" title="4.shell中变量的引用："></a>4.shell中变量的引用：</h3><ul>
<li><p>在bash中有很多特殊字符，这些特殊字符就具有特殊含义。引用就是通知shell将这些特殊字符当作普通字符来处理。</p>
<table>
<thead>
<tr>
<th>符号</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>转义字符 “\”</td>
<td>如果将\放到特殊字符前面，shell就忽略这些特殊字符的原有含义，把它们当作普通字符对待</td>
</tr>
<tr>
<td>单引号</td>
<td>如果将字符串放到一对单引号之间，那么<strong>字符串中所有字符的特殊含义被忽略</strong></td>
</tr>
<tr>
<td>双引号</td>
<td>双引号的引用与单引号基本相同，包含在双引号内的大部分特殊字符可以当作普通字符处理，但是仍然有一些特殊字符<strong>保留</strong>自己的特殊含义，比如<strong>”$“和“\”以及“ &#96; ”</strong></td>
</tr>
</tbody></table>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost ~]# echo &quot;current_user is: $USER&quot;    # 双引号中的$没有被忽略</span><br><span class="line">current_user is: root</span><br><span class="line">[root@localhost ~]# echo &#x27;current_user is: $USER&#x27;    # 单引号中的$被忽略</span><br><span class="line">current_user is: $USER</span><br></pre></td></tr></table></figure>
</li>
<li><p>说明：反引号中的字符串将被解释为shell命令</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost ~]# echo &quot;current_user is: `whoami`&quot;    # 双引号中的反引号字符串被解释为shell命令</span><br><span class="line">current_user is: root</span><br><span class="line">[root@localhost ~]# echo &#x27;current_user is: `whoami`&#x27;    # 单引号中的反引号字符串被忽略</span><br><span class="line">current_user is: `whoami`</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="4-1-转义符"><a href="#4-1-转义符" class="headerlink" title="4.1.转义符"></a>4.1.转义符</h3><table>
<thead>
<tr>
<th>转义字符</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>\n</td>
<td>换行</td>
</tr>
<tr>
<td>\r</td>
<td>回车</td>
</tr>
<tr>
<td>\t</td>
<td>制表符</td>
</tr>
<tr>
<td>\b</td>
<td>退格</td>
</tr>
<tr>
<td>\v</td>
<td>纵向制表符</td>
</tr>
<tr>
<td>\f</td>
<td>换页符</td>
</tr>
</tbody></table>
<h3 id="6-变量的运算："><a href="#6-变量的运算：" class="headerlink" title="6.变量的运算："></a>6.变量的运算：</h3><table>
<thead>
<tr>
<th>算数运算符</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>+、- 、*、&#x2F;、%</td>
<td>求和、差、乘积，商，余数</td>
</tr>
<tr>
<td>**</td>
<td>幂运算，例如3**3是求3的立方，即27</td>
</tr>
<tr>
<td>+&#x3D;、-&#x3D;、*&#x3D;、&#x2F;&#x3D;、%&#x3D;</td>
<td>例a+&#x3D;1相当于a&#x3D;a+1</td>
</tr>
<tr>
<td>++variable、–variable</td>
<td>先将变量variable的值加1，然后再赋给variable； 先将变量variable的值减1，然后再赋给variable</td>
</tr>
<tr>
<td>位运算符&lt;&lt;、&gt;&gt;</td>
<td></td>
</tr>
<tr>
<td>&amp;、| ，~，^</td>
<td>按位与：1和1为1，其他为0；按位或：只要有1即为1，否则为0；按位非；按位异或：相同为1</td>
</tr>
</tbody></table>
<ul>
<li><p>条件表达式要放在方括号之间，并且要有空格</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[$a  ==  $b]</span><br></pre></td></tr></table></figure>
</li>
<li><p>多原生bash不支持简单的数学运算,但是可以通过其他命令来实现，例如awk和expr , expr最常用</p>
<table>
<thead>
<tr>
<th>运算命令</th>
<th>意义</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>(())</td>
<td>用于整数运算的常用运算符</td>
<td>在(())中使用变量时可以去掉变量前的$符号</td>
</tr>
<tr>
<td>let</td>
<td>用于整数运算</td>
<td>使用let命令可以执行一个或者多个算术表达式，其中的变量名毋需使用$符号</td>
</tr>
<tr>
<td>expr</td>
<td>可用于整数运算</td>
<td>使用expr时，运算符及用于计算的数字左右都至少有一个空格</td>
</tr>
<tr>
<td>bc</td>
<td>适合整数及小数运算</td>
<td></td>
</tr>
<tr>
<td>$[]</td>
<td>用于整数运算</td>
<td></td>
</tr>
<tr>
<td>awk</td>
<td>既可以整数也可以用于小数</td>
<td></td>
</tr>
<tr>
<td>declare</td>
<td></td>
<td></td>
</tr>
</tbody></table>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">num1=10</span><br><span class="line">num2=2</span><br><span class="line">echo &quot;two bracket is $(($num1+$num2))&quot;       # (())用于整数运算的常用运算符</span><br><span class="line">let num3=$num1*$num2                         # let用于整数运算</span><br><span class="line">echo &quot;let is $num3&quot;                          </span><br><span class="line">echo &quot;expr is `expr $num1 \| $num2`&quot;         # expr可用于整数运算 </span><br><span class="line">echo &quot;square bracket is $[$num2**$num1]&quot;     # $[]用于整数运算</span><br><span class="line">declare -i num4=$num1/$num2                  # declare -i定义整形变量，做运算</span><br><span class="line">echo &quot;declare is $num4&quot;</span><br><span class="line">echo -n &quot;bc is &quot;;echo &quot;$num2 / $num1&quot; | bc   # bc：seq生成数字序列（这里是1~10），-s指定分隔符（这里是+）</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">awk &#x27;BEGIN &#123;print 2+3*2&#125;&#x27;      # awk既可以整数也可以用于小数</span><br></pre></td></tr></table></figure>
</li>
<li><p>expr是一款表达式计算工具，使用它能完成表达式的求值操作：（注意使用的是反引号）</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">val=&#x27;expr2 + 2&#x27;    </span><br><span class="line">num3=`expr \( 2 + 3 \) \* 4`      # 使用expr在遇到除&quot;+,-&quot;之外，都需要加转义符&quot;\&quot;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="7-表达式："><a href="#7-表达式：" class="headerlink" title="7.${ }表达式："></a>7.${ }表达式：</h3><table>
<thead>
<tr>
<th>表达式</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>${parameter}</td>
<td>返回变量的内容</td>
</tr>
<tr>
<td>$</td>
<td></td>
</tr>
</tbody></table>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="http://example.com">阿尔托莉雅</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="http://example.com/2023/03/13/3.linux/A4.Shell%E8%84%9A%E6%9C%AC/">http://example.com/2023/03/13/3.linux/A4.Shell%E8%84%9A%E6%9C%AC/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Linux/">Linux</a></div><div class="post_share"><div class="social-share" data-image="/images/Otherwallpaper/avatar.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/03/13/3.linux/B1.Docker%E5%AE%B9%E5%99%A8/" title="Docker容器"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">Docker容器</div></div></a></div><div class="next-post pull-right"><a href="/2023/03/13/3.linux/A3.Linux%E4%B8%AD%E7%BA%A7%E5%91%BD%E4%BB%A4/" title="Linux中级命令"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">Linux中级命令</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>Related Articles</span></div><div class="relatedPosts-list"><div><a href="/2023/03/13/3.linux/A1.linux%E5%9F%BA%E7%A1%80/" title="Linux基础"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">Linux基础</div></div></a></div><div><a href="/2023/03/13/3.linux/A2.Linux%E5%88%9D%E7%BA%A7%E5%91%BD%E4%BB%A4/" title="Linux初级命令"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">Linux初级命令</div></div></a></div><div><a href="/2023/03/13/3.linux/A3.Linux%E4%B8%AD%E7%BA%A7%E5%91%BD%E4%BB%A4/" title="Linux中级命令"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">Linux中级命令</div></div></a></div><div><a href="/2023/03/13/3.linux/B1.Docker%E5%AE%B9%E5%99%A8/" title="Docker容器"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">Docker容器</div></div></a></div><div><a href="/2023/03/13/3.linux/C1.git%E5%B7%A5%E5%85%B7/" title="git工具"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">git工具</div></div></a></div><div><a href="/2023/03/13/3.linux/D1.Ansible%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E5%B7%A5%E5%85%B7/" title="Ansible基础"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-13</div><div class="title">Ansible基础</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/images/Otherwallpaper/avatar.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">阿尔托莉雅</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">11</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">2</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://toscode.gitee.com/blue-fantasy" target="_blank" title="Gitee"><i class="fab fa-github"></i></a><a class="social-icon" href="/1095322098@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-Shell%E7%9A%84%E6%A6%82%E5%BF%B5%EF%BC%9A"><span class="toc-number">1.</span> <span class="toc-text">1.Shell的概念：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-Shell%E8%A7%A3%E6%9E%90%E5%99%A8%EF%BC%9A"><span class="toc-number">2.</span> <span class="toc-text">2.Shell解析器：</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E6%99%AE%E9%80%9A%E5%8F%98%E9%87%8F%E7%9A%84%E5%AE%9A%E4%B9%89%E4%B8%8E%E4%BD%BF%E7%94%A8%EF%BC%9A"><span class="toc-number">2.1.</span> <span class="toc-text">1.普通变量的定义与使用：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E7%9A%84%E5%AE%9A%E4%B9%89%E4%B8%8E%E4%BD%BF%E7%94%A8%EF%BC%9A"><span class="toc-number">2.2.</span> <span class="toc-text">2.环境变量的定义与使用：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%91%BD%E4%BB%A4%E5%88%AB%E5%90%8D%EF%BC%9A"><span class="toc-number">2.3.</span> <span class="toc-text">3.命令别名：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E5%91%BD%E4%BB%A4%E5%8E%86%E5%8F%B2%EF%BC%9A"><span class="toc-number">2.4.</span> <span class="toc-text">4.命令历史：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E5%86%85%E9%83%A8%E5%91%BD%E4%BB%A4%EF%BC%9A"><span class="toc-number">2.5.</span> <span class="toc-text">5.内部命令：</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-shell%E8%84%9A%E6%9C%AC%E5%85%A5%E9%97%A8%EF%BC%9A"><span class="toc-number">3.</span> <span class="toc-text">3.shell脚本入门：</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E7%BC%96%E5%86%99%E8%A7%84%E5%88%99%EF%BC%9A"><span class="toc-number">3.1.</span> <span class="toc-text">1.编写规则：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E9%85%8D%E7%BD%AEvim%E7%9A%84%E7%BC%96%E8%BE%91%E6%A0%BC%E5%BC%8F%EF%BC%9A"><span class="toc-number">3.2.</span> <span class="toc-text">2.配置vim的编辑格式：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E6%B3%A8%E9%87%8A%EF%BC%9A"><span class="toc-number">3.3.</span> <span class="toc-text">2.注释：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E4%BD%BF%E7%94%A8%E6%B5%81%E7%A8%8B%EF%BC%9A"><span class="toc-number">3.4.</span> <span class="toc-text">3.使用流程：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E6%89%A7%E8%A1%8C%E8%84%9A%E6%9C%AC%E7%9A%84%E6%96%B9%E6%B3%95%EF%BC%9A"><span class="toc-number">3.5.</span> <span class="toc-text">4.执行脚本的方法：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-shell%E8%84%9A%E6%9C%AC%E7%9A%84%E9%80%80%E5%87%BA%E7%8A%B6%E6%80%81%EF%BC%9A"><span class="toc-number">3.6.</span> <span class="toc-text">5.shell脚本的退出状态：</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E5%8F%98%E9%87%8F%EF%BC%9A"><span class="toc-number">4.</span> <span class="toc-text">4.变量：</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E5%8F%98%E9%87%8F%E5%90%8D%E7%9A%84%E8%A7%84%E8%8C%83%EF%BC%9A"><span class="toc-number">4.1.</span> <span class="toc-text">1.变量名的规范：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%8F%98%E9%87%8F%E7%9A%84%E7%B1%BB%E5%9E%8B%EF%BC%9A"><span class="toc-number">4.2.</span> <span class="toc-text">2.变量的类型：</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%EF%BC%9A"><span class="toc-number">4.2.0.1.</span> <span class="toc-text">自定义环境变量：</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#bash%E5%86%85%E7%BD%AE%E7%9A%84%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%EF%BC%9A"><span class="toc-number">4.2.0.2.</span> <span class="toc-text">bash内置的环境变量：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%8F%98%E9%87%8F%E7%9A%84%E5%AE%9A%E4%B9%89%E4%B8%8E%E4%BD%BF%E7%94%A8%EF%BC%9A"><span class="toc-number">4.3.</span> <span class="toc-text">3.变量的定义与使用：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-shell%E4%B8%AD%E5%8F%98%E9%87%8F%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%9A"><span class="toc-number">4.4.</span> <span class="toc-text">4.shell中变量的引用：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-%E8%BD%AC%E4%B9%89%E7%AC%A6"><span class="toc-number">4.5.</span> <span class="toc-text">4.1.转义符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-%E5%8F%98%E9%87%8F%E7%9A%84%E8%BF%90%E7%AE%97%EF%BC%9A"><span class="toc-number">4.6.</span> <span class="toc-text">6.变量的运算：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-%E8%A1%A8%E8%BE%BE%E5%BC%8F%EF%BC%9A"><span class="toc-number">4.7.</span> <span class="toc-text">7.${ }表达式：</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/13/4.%E6%95%B0%E6%8D%AE%E5%BA%93/B3.NoSQL%E6%95%B0%E6%8D%AE%E5%BA%93%20-%20MongDB/" title="NoSQL篇-MongDB数据库">NoSQL篇-MongDB数据库</a><time datetime="2023-03-13T12:32:38.445Z" title="Created 2023-03-13 20:32:38">2023-03-13</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/13/4.%E6%95%B0%E6%8D%AE%E5%BA%93/B2.NoSQL%E6%95%B0%E6%8D%AE%E5%BA%93%20-%20Redis/" title="NoSQL篇-Redis数据库">NoSQL篇-Redis数据库</a><time datetime="2023-03-13T12:32:38.441Z" title="Created 2023-03-13 20:32:38">2023-03-13</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/13/4.%E6%95%B0%E6%8D%AE%E5%BA%93/A1.MySQL-%E6%AC%A7%E9%B9%8F%E7%AF%87/" title="SQL篇-MySQL数据库">SQL篇-MySQL数据库</a><time datetime="2023-03-13T12:32:38.437Z" title="Created 2023-03-13 20:32:38">2023-03-13</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/13/3.linux/D2.Ansible%E5%89%A7%E6%9C%AC/" title="Ansible">Ansible</a><time datetime="2023-03-13T12:32:23.866Z" title="Created 2023-03-13 20:32:23">2023-03-13</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/13/3.linux/D1.Ansible%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E5%B7%A5%E5%85%B7/" title="Ansible基础">Ansible基础</a><time datetime="2023-03-13T12:32:23.861Z" title="Created 2023-03-13 20:32:23">2023-03-13</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By 阿尔托莉雅</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Toggle Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>